package task

import (
	"errors"

	"cvevulner/common"
	"cvevulner/taskhandler"

	"github.com/astaxie/beego/config"
	"github.com/astaxie/beego/logs"
)

// Sync data to cve vulnerability and create issue
func SyncCveAndIssue(list []string) error {
	syncErr := SyncCveData(list)
	if syncErr != nil {
		logs.Error("SyncCveAndIssue, SyncCveData, err: ", syncErr)
	}
	issueErr := SyncCreateIssue(list)
	if issueErr != nil {
		logs.Error("SyncCveAndIssue, SyncCreateIssue, err: ", issueErr)
		return issueErr
	}
	return nil
}

func SyncCveData(list []string) error {
	defer common.Catchs()
	logs.Info("Start the task of generating cve raw data into cve library.")
	// Query the cve to be processed, 1: add; 2: modify
	BConfig, err := config.NewConfig("ini", "conf/app.conf")
	if err != nil {
		logs.Error("config init, error: ", err)
		return err
	}
	// The amount of data processed at a time
	prcNum, err := BConfig.Int("crontab::prcnum")
	if err != nil {
		logs.Error("crontab::prcnum, error:  ", err)
		return errors.New("value is nil")
	}
	// How many days have been processed to date data
	days := -1
	// openeuler Number start value
	cveRef := BConfig.String("cve::cveref")
	openeulerNum, oErr := BConfig.Int("cve::openeulernum")
	if oErr != nil {
		logs.Error("cve::openeulernum, err:", err)
		return oErr
	}
	// cve credibility level
	credibilityLevel, cErr := BConfig.Int("cve::credibility_level")
	if cErr != nil {
		logs.Error("cve::credibility_level, err:", err)
		return cErr
	}
	// Process raw data obtained by api
	ok, syncErr := taskhandler.GetCveOriginData(prcNum, days, openeulerNum, credibilityLevel, cveRef, list)
	if !ok || syncErr != nil {
		logs.Error("ProcCveOriginData, GetCveOriginData, err: ", err)
	}
	return syncErr
}

func SyncCreateIssue(list []string) error {
	defer common.Catchs()
	logs.Info("The issue task of creating cve begins. . .")
	// Query the cve to be processed, 1: add; 2: modify
	BConfig, err := config.NewConfig("ini", "conf/app.conf")
	if err != nil {
		logs.Error("CreateIssue, config init, error:", err)
		return err
	}
	days := -1
	years, ok := BConfig.Int("cve::cve_number")
	if ok != nil {
		logs.Error("CreateIssue, config cve::years, error:", ok)
		return ok
	}
	manYears, ok := BConfig.Int("cve::cve_number_m")
	if ok != nil {
		logs.Error("CreateIssue, config cve::manYears, error:", ok)
		return ok
	}
	toolYears, ok := BConfig.Int("cve::cve_number_t")
	if ok != nil {
		logs.Error("CreateIssue, config cve::toolYears, error:", ok)
		return ok
	}
	prcnum, ok := BConfig.Int("crontab::prcnum")
	if ok != nil {
		logs.Error("CreateIssue, config crontab::prcnum, error:", ok)
		return ok
	}
	issueWhitelist, ok := BConfig.Int("cve::issue_whitelist")
	if ok != nil {
		logs.Error("CreateIssue, config cve::issue_whitelist, error: ", ok)
		return ok
	}
	beforeTime := common.GetBeforeTime(days)
	if issueWhitelist == 1 {
		limitErr := addLimitedIssue(beforeTime, prcnum, years, toolYears, manYears)
		logs.Info("CreateIssue, addLimitedIssue, err: ", limitErr)
		logs.Info("The execution of the issue task of creating cve ends. . .")
		return limitErr
	} else {
		unlimitErr := addUnlimitedIssue(beforeTime, prcnum, years, toolYears, manYears, 1, list)
		logs.Info("CreateIssue, addUnlimitedIssue, err: ", unlimitErr)
		logs.Info("The execution of the issue task of creating cve ends. . .")
		return unlimitErr
	}
}
